package gk.example.activity;

import gk.example.App;
import gk.example.BaseActivity;
import gk.example.adapter.PersonalInfoAdapter;
import gk.example.util.ImageLoadUtil;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;

import com.example.gkdate.R;

import de.hdodenhof.circleimageview.CircleImageView;

public class PersonalInfoActivity extends BaseActivity implements
		OnClickListener {
	private ListView mlvPersonalInfo;
	private CircleImageView mCircleImageView;
	private TextView mTvTopTitle;
	private ImageButton mBtnBack;

	@Override
	protected void initView() {
		setContentView(R.layout.activity_personal_info);
		mTvTopTitle = (TextView) findViewById(R.id.tv_top_title);
		mBtnBack = (ImageButton) findViewById(R.id.btn_back);
		mlvPersonalInfo = (ListView) findViewById(R.id.lv_personal_info);
		mCircleImageView = (CircleImageView) findViewById(R.id.iv_personal_info_head);

	}

	@Override
	protected void initEvent() {
		mTvTopTitle.setText(getResources().getString(R.string.personal_info));
		mBtnBack.setOnClickListener(this);
		mlvPersonalInfo.setAdapter(new PersonalInfoAdapter(this,
				getSupportFragmentManager()));
		mCircleImageView.setOnClickListener(this);
		if (App.BmobUser.getUserHead() != null) {
			ImageLoadUtil.getInstance().setImageBackground(this, false,
					mCircleImageView,
					App.BmobUser.getUserHead().getFileUrl(this), null, 64, 64);
		} else {
			mCircleImageView.setImageResource(R.drawable.default_round_head);
		}
		mlvPersonalInfo.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				switch (position) {
				case 0:
					break;

				case 3:
					break;
				default:
					Intent intent = new Intent(PersonalInfoActivity.this,
							PersonalInfoChangeActivity.class);
					Bundle bundle = new Bundle();
					bundle.putInt("position", position);
					switch (position) {
					case 1:
						bundle.putString("data", App.BmobUser.getNickName());
						break;
					case 4:
						if (App.BmobUser.getEmail() != null) {
							bundle.putString("data", App.BmobUser.getEmail());
						}
						break;
					case 5:
						if (App.BmobUser.getIntroduction() != null) {
							bundle.putString("data",
									App.BmobUser.getIntroduction());
						}
					}
					intent.putExtras(bundle);
					startActivity(intent);
					finish();
					break;
				}

			}
		});
	}

	/** 拍照 **/
	private static final int PHOTO_REQUEST_CAMERA = 1;
	/** 相册 **/
	private static final int PHOTO_REQUEST_ALBUM = 2;

	/** 裁剪结果 **/
	private File mPhotoFile;
	private static final String MPHOTOPATH = Environment
			.getExternalStorageDirectory() + "/GkDate/";

	/**
	 * 
	 * 描述：更换头像
	 * 
	 * @return void
	 */
	private void showChangeHeadDialog() {
		AlertDialog.Builder builder = new Builder(this);
		builder.setTitle("提示");
		builder.setItems(new String[] { "拍照", "本地相册" },
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						File mCreateDir = new File(MPHOTOPATH);
						if (!mCreateDir.exists()) {
							mCreateDir.mkdirs();
						}
						switch (which) {
						// 拍照
						case 0:
							mPhotoFile = new File(MPHOTOPATH,
									getPhotoFileName());
							Intent intentCamera = new Intent(
									MediaStore.ACTION_IMAGE_CAPTURE);
							// 指定调用相机拍照后照片的储存路径
							intentCamera.putExtra(MediaStore.EXTRA_OUTPUT,
									Uri.fromFile(mPhotoFile));
							startActivityForResult(intentCamera,
									PHOTO_REQUEST_CAMERA);
							break;
						// 本地相册
						case 1:
							Intent intentAlbum = new Intent(Intent.ACTION_PICK,
									null);
							intentAlbum
									.setDataAndType(
											MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
											"image/*");
							startActivityForResult(intentAlbum,
									PHOTO_REQUEST_ALBUM);
							break;
						default:
							break;
						}

					}
				});
		builder.setPositiveButton("取消",
				new android.content.DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dialog.dismiss();
					}
				});
		builder.create().show();

	}

	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		Bundle bundle = new Bundle();
		Intent intent = new Intent(this, ClipActivity.class);
		switch (requestCode) {
		case PHOTO_REQUEST_CAMERA:
			bundle.putString("photoPath", mPhotoFile.getPath());
			intent.putExtras(bundle);
			startActivity(intent);
			finish();
			break;
		case PHOTO_REQUEST_ALBUM:
			if (data != null) {
				Uri selectedImage = data.getData(); // 获取系统返回的照片的Uri
				String[] filePathColumn = { MediaColumns.DATA };
				Cursor cursor = getContentResolver().query(selectedImage,
						filePathColumn, null, null, null);// 从系统表中查询指定Uri对应的照片
				cursor.moveToFirst();
				int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
				String picturePath = cursor.getString(columnIndex);
				mPhotoFile = new File(picturePath);
				cursor.close();
				bundle.putString("photoPath", mPhotoFile.getPath());
				intent.putExtras(bundle);
				startActivity(intent);
				finish();
			}
			break;

		default:
			break;
		}
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * 
	 * 描述：使用日期来为照片命名
	 * 
	 * @return 返回照片文件名
	 * @return String
	 */
	private String getPhotoFileName() {
		Date date = new Date(System.currentTimeMillis());
		SimpleDateFormat dateFormat = new SimpleDateFormat(
				"'IMG'_yyyyMMdd_HHmmss");
		return dateFormat.format(date) + ".JPG";
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if (keyCode == KeyEvent.KEYCODE_BACK) {
			startActivity(new Intent(this, MainActivity.class));
			finish();
		}
		return super.onKeyDown(keyCode, event);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.iv_personal_info_head:
			showChangeHeadDialog();
			break;
		default:
			break;
		}
	}
}
